package com.github.edu.web.organization.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.github.admin.edu.assembly.common.entity.JsonEntity;
import com.github.admin.edu.assembly.common.util.JsonUtils;
import com.github.admin.edu.assembly.date.util.DateFormatUtils;
import com.github.admin.edu.assembly.string.util.StringUtils;
import com.github.admin.edu.oauth.server.api.entity.TSysOrganization;
import com.github.edu.client.common.service.impl.BaseAbstractService;
import com.github.edu.web.organization.domain.OrganizationManagerDomain;
import com.github.edu.web.organization.service.OrganizationManagerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
import java.text.ParseException;
import java.util.Date;

/**
 * Create by IntelliJ IDEA
 * 用户：王建
 * 日期：2018-8-2
 */
@Service
@Slf4j
public class OrganizationManagerServiceImpl extends BaseAbstractService implements OrganizationManagerService {

    @Autowired
    private OrganizationManagerDomain domain;

    @Override
    public String getAllTree() {
        return domain.getAllEntityByTree();
    }

    @Override
    public void save(TSysOrganization organization, ModelMap map) {

        map.put("msg","保存失败！");

        map.put("button",true);

        try {
            organization.setCreatedate(DateFormatUtils.formatDate(new Date(),"yyyy/MM/dd HH:mm"));
        } catch (ParseException e) {
            log.error("date format error ! "+e.getLocalizedMessage());
        }

        if(null==organization.getId()||0==organization.getId()){
            organization.setId(Integer.parseInt(getId(organization.getClass().getSimpleName()).toString()));
            if(-1==organization.getParentid()){
                organization.setTreeCode(String.valueOf(organization.getId()));
            }else {
                organization.setTreeCode(organization.getParentCode()+"-"+organization.getId());
            }

        }

        JsonEntity<TSysOrganization> jsonEntity=new JsonEntity<>();

        jsonEntity.setData(organization);

        String code=domain.saveOrUpdate(JsonUtils.toJson(jsonEntity));

        if(StringUtils.isNotBlank(code)){

            JsonEntity<TSysOrganization> entity=JsonUtils.toCollection(code, new TypeReference<JsonEntity<TSysOrganization>>() {});

            if(null!=entity){
                if(200==entity.getState()){

                    organization=entity.getData();

                    map.put("msg","");

                    map.put("button",false);

                }else {
                    map.put("msg",entity.getMsg());
                }

            }
        }

        map.put("entity",organization);

    }

    /**
     * 删除节点
     * @param id
     * @return
     */
    @Override
    public String delete(Integer id) {

        return domain.deleteByRootId(id);
    }

    @Override
    public void openEntity(ModelMap map, Integer id) {
        TSysOrganization sysOrganization=null;
        String code=domain.getEntity(id);
        if(StringUtils.isNotBlank(code)){
            JsonEntity<TSysOrganization> jsonEntity=JsonUtils.toCollection(code, new TypeReference<JsonEntity<TSysOrganization>>() {
            });
            if(null!=jsonEntity){
                sysOrganization=jsonEntity.getData();
            }
        }
        if(null==sysOrganization){
            sysOrganization=new TSysOrganization();
        }
        map.put("entity",sysOrganization);
    }

    @Override
    public void index(ModelMap map) {
        String code=domain.findOrgByFirst();
        TSysOrganization sysOrganization=null;
        if(StringUtils.isNotBlank(code)){
            JsonEntity<TSysOrganization> jsonEntity=JsonUtils.toCollection(code, new TypeReference<JsonEntity<TSysOrganization>>() {
            });
            if(null!=jsonEntity){
               sysOrganization =jsonEntity.getData();
            }
        }
        if(null==sysOrganization){
            sysOrganization=new TSysOrganization();
        }
        map.put("entity",sysOrganization);
    }

    @Override
    public void openChild(ModelMap map, Integer pid) {
        String code =domain.getEntity(pid);
        TSysOrganization tSysOrganization=new TSysOrganization();
        tSysOrganization.setParentid(pid);
        tSysOrganization.setType(2);
        if(StringUtils.isNotBlank(code)){
            JsonEntity<TSysOrganization> jsonEntity=JsonUtils.toCollection(code, new TypeReference<JsonEntity<TSysOrganization>>() {
            });
            if(null!=jsonEntity){
                TSysOrganization sysOrganization=jsonEntity.getData();
                if(null!=sysOrganization){
                    tSysOrganization.setParentName(sysOrganization.getName());
                }
            }
        }
        map.put("entity",tSysOrganization);
    }
}
